<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 二叉树的最小深度
        function Node(val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
        var a = new Node(1)
        var b = new Node(null)
        var c = new Node(2)
        var d = new Node(3)
        var e = new Node(4)
        var f = new Node(5)
        var g = new Node(6)
        a.left = b
        a.right = c
        b.left = d
        b.right = e
        c.left = f
        c.right = g
        /* 
            时间复杂度：O(n)
            空间复杂度：O(n)
        */
        var maxDepth = function (root) {
            let depth = 0
            function traverse (node, level) {
                if (!node) return
                depth = Math.max(level, depth)
                traverse(node.left, level + 1)
                traverse(node.right, level + 1)
            }
            traverse(root, 1)
            return depth
        };
        console.log(maxDepth(a));
    </script>
</body>

</html>